public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        // 滑动窗口，用left和right维护一个窗口，窗口中都是不存在重复字符的字串
        // 当窗口中出现重复的字符的时候，就统计出窗口的长度，然后移动left将重复的字符移出去
        int length = s.length();
        if (length == 0) return 0;
        int left = 0, right = 0;
        int[] hash = new int[128];
        int maxLength = 1;
        while (right < length) {
            while (right < length && hash[s.charAt(right)] == 0) {
                hash[s.charAt(right)] = 1;
                right++;
            }
            maxLength = Math.max(maxLength,right-left);
            while (right < length && left < right && s.charAt(left) != s.charAt(right)) {
                hash[s.charAt(left)] = 0;
                left++;
            }
            left++;
            right++;
        }
        return maxLength;
    }

    public static void main(String[] args) {
        LengthOfLongestSubstring lengthOfLongestSubstring = new LengthOfLongestSubstring();
        int length = lengthOfLongestSubstring.lengthOfLongestSubstring("abcabcbb");
        System.out.println(length);
    }
}
